package com.tevintao.marketingsystem.task;


import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.HookWindowManagerGlobal;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashSet;

public class WxUiViewConfig {

    public static final String KEY_USER_NAME = "key_user_name";
    public static final String KEY_PASSWORD = "key_password";
    public static final String KEY_SEARCH_FRIEND_KEY_WORDS = "key_search_friend_key_words";//加好友的时候，搜索对方的电话号码
    public static final String KEY_NEED_CHAT_NICK_NAME = "key_need_chat_nick_name";//相互聊天，对方的昵称
    public static final String KEY_CHAT_CONTENT = "key_chat_content";//相互聊天，内容
    public static final String KEY_FORWARD_SUBSCRIBE_JAVA_SCRIBE = "key_forward_subscribe_java_scribe";//订阅号，历史消息，执行的js
    public static final String KEY_FRIEND_CIRCLE_PATHS = "key_friend_circle_paths";
    public static final String KEY_FRIEND_CIRCLE_ID = "key_friend_circle_id";
    public static final String KEY_CONTACTS_PHONES = "key_contacts_phones";
    public static final String KEY_MY_SIGNATURE = "key_my_signature";
    public static final String CHANGE_ICON_PATH = "change_icon_path";

    public static final String KEY_SEND_PIC_MESSAGE = "KEY_SEND_PIC_MESSAGE";

    public static final String DIALOG_CLASS = "android.view.ContextThemeWrapper"; //对话框

    public static final int DIALOG_INFO_TEXT = 2131625950;

    //取消按钮,安装更新
    public static final int DIALOG_INFO_CANCEL = 2131626641;
    public static final int DIALOG_INFO_OK = 2131626642;

    public static final int CHANGE_COVER_ITEM = 2131624112;
    /**
     * 主界面，包括登录注册，和tab页面
     */
    public static final String MAIN_LAUNCH = "com.tencent.mm.ui.LauncherUI";
    public static final int MORE_FUNCTION = 2131624799;
    public static final int SEND_PIC = 2131624247;
    /**
     * 登录，注册界面，登录按钮ID
     */
    public static final int FIRST_LOGIN_BUTTON = 2131627432;
    /**
     * 国际区号编码
     */
    public static final int FIRST_LOGIN_INTERNATIONAL_CODE_EDIT_TEXT = 2131624400;
    /**
     * 电话号码输入框
     */
    public static final int FIRST_LOGIN_PHONE_EDIT_TEXT = 2131626652;
    /**
     * 密码输入框
     */
    public static final int FIRST_LOGIN_PASSWORD_EDIT_TEXT = 2131624155;
    /**
     * 登录按钮
     */
    public static final int FIRST_LOGIN_LOGIN_BUTTON = 2131625054;

    public static final int TAB_WEIXIN = 0;
    public static final int TAB_CONTACTS = 1;
    public static final int TAB_DISCOVER = 2;
    public static final int TAB_ME = 3;

    public static final int TAB_NAME_TEXT_VIEW = 2131626647;

    public static final int TAB_NAME_SELECTED_COLOR = -12206054;
    public static final int TAB_NAME_NO_SELECTED_COLOR = -6710887;

    public static final int MY_NAME_ITEM = 2131626780;

    public static final int TAB_ICO_NEW_MESSAGE_NUM = 2131626645;
    public static final int LIST_ITEM_NEW_MESSAGE_NUM = 2131624200;

    /**
     * 判断是否在特定tab下面，不再就自己跳转，
     *
     * @param view     main launcher view
     * @param tabIndex tab标签位置，0，TAB_WEIXIN 微信，1，TAB_CONTACTS 通讯录，2，TAB_DISCOVER 发现 3，TAB_ME我
     * @return 是否在tab，不在就自动跳转
     */
    @SuppressWarnings("ResourceType")
    public static boolean inTab(View view, int tabIndex) {
        if (null == view) {
            return false;
        }
        View tagView = view.findViewWithTag(tabIndex);
        if (null != tagView) {
            LogManager.getInstance().logInfo("tagView = " + tagView);
//            JSONObject joTree = HookWindowManagerGlobal.GetViewJsonTree((ViewGroup) tagView, -1);
//            HookWindowManagerGlobal.printViewTreeJson(joTree, 0);
            View tabNameView = tagView.findViewById(TAB_NAME_TEXT_VIEW);
            if (null != tabNameView && tabNameView instanceof TextView) {
                TextView tabNameTextView = (TextView) tabNameView;
                int color = tabNameTextView.getTextColors().getDefaultColor();
//                LogManager.getInstance().logInfo("color = " + color);
                if (TAB_NAME_NO_SELECTED_COLOR == color) {
                    ViewOperation.clickView(tagView);
                    return false;
                } else if (TAB_NAME_SELECTED_COLOR == color) {
//                    LogManager.getInstance().logInfo("inTab " + tabIndex);
                    return true;
                }
            }
        }
        return false;
    }

    public static final int DISCOVER_LIST_ITEM_ID = 16908310;

    public static View getViewByIdAndContent(ViewGroup viewGroup, int id, String content) {
        if (null == viewGroup) {
            return null;
        }
        for (int i = 0, len = viewGroup.getChildCount(); i < len; i++) {
            View v = viewGroup.getChildAt(i);
            if (v instanceof ViewGroup) {
                View getView = getViewByIdAndContent((ViewGroup) v, id, content);
                if (null != getView) {
                    return getView;
                }
            } else {
                if (id == v.getId() && v instanceof TextView) {
                    TextView textView = (TextView) v;
                    if (textView.getText().toString().equals(content)) {
                        return v;
                    }
                }
            }
        }
        return null;
    }

    public static View getViewByIdAndContainContent(ViewGroup viewGroup, int id, String content) {
        if (null == viewGroup) {
            return null;
        }
        for (int i = 0, len = viewGroup.getChildCount(); i < len; i++) {
            View v = viewGroup.getChildAt(i);
            if (v instanceof ViewGroup) {
                View getView = getViewByIdAndContainContent((ViewGroup) v, id, content);
                if (null != getView) {
                    return getView;
                }
            } else {
                if (id == v.getId() && v instanceof TextView) {
                    TextView textView = (TextView) v;
                    if (textView.getText().toString().contains(content)) {
                        return v;
                    }
                }
            }
        }
        return null;
    }

    public static final String MAIN_LAUNCHER_SEARCH_CLASS_NAME = "ActionMenuItemView";

    public static View getViewByClassName(ViewGroup viewGroup, String className) {
        if (null == viewGroup) {
            return null;
        }
        for (int i = 0, len = viewGroup.getChildCount(); i < len; i++) {
            View v = viewGroup.getChildAt(i);
            if (v instanceof ViewGroup) {
                View getView = getViewByClassName((ViewGroup) v, className);
                if (null != getView) {
                    return getView;
                }
            } else {
                if (v.getClass().getSimpleName().equals(className)) {
                    return v;
                }
            }
        }
        return null;
    }

    public static void getViewsById(ViewGroup viewGroup, int id, ArrayList<View> views) {
        if (null == viewGroup) {
            return;
        }
        for (int i = 0, len = viewGroup.getChildCount(); i < len; i++) {
            View v = viewGroup.getChildAt(i);
            if (v.getId() == id) {
                views.add(v);
            } else if (v instanceof ViewGroup) {
                getViewsById((ViewGroup) v, id, views);
            }
        }

    }

    public static final int CHAT_LIST_VIEW = 2131626427;
    public static final int CHAT_LIST_ITEM = 2131625208;//每一个聊天联系人聊天item
    public static final int CHAT_VIEW_GROUP_VIEW = 2131623993;//每一个聊天联系人聊天item
    public static final int CHAT_VIEW_EDIT_TEXT = 2131624794;//聊天界面里面的输入框
    public static final int CHAT_VIEW_SEND_BUTTON = 2131624800;//聊天界面里面的发送按钮
    public static final int CHAT_VIEW_NICK_NAME_TITLE = 2131624143;//聊天界面里面的标题
    public static final int CHAT_VIEW_BACK_BUTTON = 2131624142;//聊天界面里面的发送按钮

    public static boolean clickSearchButton(ViewGroup viewGroup) {
        if (null == viewGroup) {
            return false;
        }
        LogManager.getInstance().logInfo("前几页没有找到需要聊天的人");
        View searchView = WxUiViewConfig.getViewByClassName(viewGroup, WxUiViewConfig.MAIN_LAUNCHER_SEARCH_CLASS_NAME);
        LogManager.getInstance().logInfo("searchView = " + searchView);
        if (null != searchView) {
            ViewOperation.clickView(searchView);
            return true;
        }
        return false;
    }

    public static final String PHONE_LOGIN_VIEW = "com.tencent.mm.ui.account.mobile.MobileInputUI";//帐号密码登录页面

    public static final String UPDATE_VERSION = "com.tencent.mm.sandbox.updater.AppInstallerUI";

    public static final String CIRCLE_OF_FRIENDS = "com.tencent.mm.plugin.sns.ui.SnsTimeLineUI";
    public static final String CIRCLE_OF_FRIENDS_NEW_MESSAGE = "com.tencent.mm.plugin.sns.ui.SnsMsgUI"; //朋友圈新消息


    public static final int CIRCLE_OF_FRIENDS_LIST_VIEW = 2131627676;
    public static final int CIRCLE_OF_FRIENDS_OPEN_MENU = 2131627661;
    public static final int CIRCLE_OF_FRIENDS_COMMENT = 2131627643;
    public static final int CIRCLE_OF_FRIENDS_AGREE = 2131627640;

    public static final int CIRCLE_OF_FRIEND_COVER = 2131627701;

    public static final String VIEW_MAIN_LAUNCHER_SEARCH = "com.tencent.mm.plugin.search.ui.FTSMainUI";//打开搜索页面后

    public static final int ID_SEARCH_EDIT_TEXT = 2131624155;
    public static final int ID_SEARCH_RESULT_CONTENT = 2131624184;

    public static final int ID_SEARCH_NICK_NAME_RESULT_CONTENT = 2131624268;

    public static final int ID_ACCEPT_FRIEND_LIST_ITEM = 2131625697;//接受好友，有新消息提示
    public static final int ID_NEW_FRIEND_LIST_ITEM = 2131625691;//通讯录列表页面，新的朋友Item
    public static final int ID_NEW_FRIEND_LIST_VIEW = 2131625708;//通讯录列表页面，新的朋友ListView
    public static final int ID_NEW_FRIEND_LIST_ITEM_NICK_NAME = 2131625706;//新的朋友列表，item里面的昵称
    //    public static final int ID_NEW_FRIEND_LIST_ITEM_ADD = 2131625702;//新的朋友列表，item里面的添加按钮
    public static final int ID_NEW_FRIEND_LIST_ITEM_ACCEPT = 2131625703;//新的朋友列表，item里面的接受按钮
    public static final int ID_NEW_FRIEND_LIST_ITEM_HAVE_ADD = 2131625705;//新的朋友列表，item里面的接受按钮


    public static final String VIEW_CONTACT_INFO_UI = "com.tencent.mm.plugin.profile.ui.ContactInfoUI";//联系人资料页面
    public static final int ID_CONTACTS_INFO_SEND = 2131625120;//发消息按钮
    public static final int ID_CONTACTS_INFO_SAY_HI = 2131625116;//打招呼
    public static final int ID_CONTACTS_INFO_ADD = 2131625119;//添加到通讯录

    public static final String CHAT_UI = "com.tencent.mm.ui.chatting.ChattingUI";
    public static final int KEY_CHAT_UI_LIST_VIEW = 2131624768;
    public static final String VIEW_SUBSCRIBE_LIST = "com.tencent.mm.ui.conversation.BizConversationUI";//
    public static final int ID_SUBSCRIBE_LIST_BACK = 2131624142;

    public static final String VIEW_NEW_FRIEND_LIST = "com.tencent.mm.plugin.subapp.ui.friend.FMessageConversationUI";
    public static final int ADD_FRIEND = 2131625702;
    public static final int ADD_PHONE_CONTACTS = 2131625716;
    public static final String VIEW_CANCELLATION_VIEW = "com.tencent.mm.ui.tools.AccountDeletedAlphaAlertUI";//
    public static final String VIEW_LOGIN_AGAIN = "com.tencent.mm.ui.account.LoginHistoryUI";//再次登录页面
    public static final int ID_LOGIN_AGAIN_PASSWORD = 2131626232;
    public static final int ID_LOGIN_AGAIN_LOGIN = 2131626233;
    public static final String VIEW_SHAKE_SHAKE = "com.tencent.mm.plugin.shake.ui.ShakeReportUI";//摇一摇
    public static final String VIEW_SECURITY = "com.tencent.mm.plugin.safedevice.ui.SecurityAccountIntroUI";//手机验证页面

    public static void printViewGroup(ViewGroup v) {
        JSONObject joTree = HookWindowManagerGlobal.GetViewJsonTree(v, -1);
        HookWindowManagerGlobal.printViewTreeJson(joTree, 0);
    }

    @SuppressWarnings("ResourceType")
    public static boolean haveLogin(String contextName, View v) {
        if (mNoLoginView.contains(contextName)) {
            return false;
        }
        if (MAIN_LAUNCH.equals(contextName)) {
            View loginView = v.findViewById(FIRST_LOGIN_BUTTON);
            return !(null != loginView && loginView.getVisibility() == View.VISIBLE);
        }
        return true;
    }

    private static HashSet<String> mNoLoginView = new HashSet<>();

    static {
        mNoLoginView.add(DIALOG_CLASS);
        mNoLoginView.add(PHONE_LOGIN_VIEW);
        mNoLoginView.add(VIEW_CANCELLATION_VIEW);
        mNoLoginView.add(VIEW_LOGIN_AGAIN);
        mNoLoginView.add(VIEW_SECURITY);
    }

    public static final String VIEW_NEARBY_LIST_VIEW = "com.tencent.mm.plugin.nearby.ui.NearbyFriendsUI";
    public static final int KEY_NEARBY_FAILED_VIEW = 2131626836;
    public static final int KEY_NEARBY_BACK_VIEW = 2131624160;

    public static final String VIEW_NEARBY_SAY_HI = "com.tencent.mm.plugin.nearby.ui.NearbyFriendShowSayHiUI";
    public static final int KEY_NEARBY_LOOK_BUTTON_VIEW = 2131626837;
    public static final int KEY_NEARBY_SAY_HI_FRIEND = 2131626038;
    public static final String VIEW_NEARBY_OTHER_FRIEND_SAY_HI = "com.tencent.mm.plugin.nearby.ui.NearbySayHiListUI";

    public static final String VIEW_SEARCH_FRIEND_SAY_HI = "com.tencent.mm.plugin.profile.ui.SayHiWithSnsPermissionUI";

    public static final int KEY_SEARCH_FRIEND_SAY_HI_TEXT_VIEW = 2131627356;
    public static final int KEY_SEARCH_FRIEND_SAY_HI_SEND = 2131624139;

    public static final String VIEW_SEARCH_SUBSCRIBE_LIST_WEB_VIEW = "com.tencent.mm.plugin.webview.ui.tools.fts.FTSBizWebViewUI";
    public static final int KEY_WEB_VIEW_PARENT = 2131625783;
    public static final String VIEW_SELECT_ADD_FRIEND_TYPE = "com.tencent.mm.plugin.subapp.ui.pluginapp.AddMoreFriendsUI";
    public static final String VIEW_SELECT_ADD_FRIEND_TYPE_2 = "com.tencent.mm.plugin.subapp.ui.pluginapp.AddMoreFriendsByOtherWayUI";
    public static final int KEY_TYPE_PHONE_CONTACTS = 16908310;
    public static final String VIEW_LOOK_PHONE_CONTACTS_LIST = "com.tencent.mm.ui.bindmobile.MobileFriendUI";
    public static final int KEY_NO_CONTACTS = 2131625663;
    public static final int KEY_NO_RESULT = 2131625664;
    public static final String VIEW_ME_INFO = "com.tencent.mm.plugin.setting.ui.setting.SettingsPersonalInfoUI";
    public static final String VIEW_EDIT_SIGNATURE = "com.tencent.mm.plugin.setting.ui.setting.EditSignatureUI";
    public static final int INPUT_SIGN_EDIT = 2131624199;
    public static final int SAVE_SIGN_BUTTON = 2131624139;
    public static final String VIEW_ALBUM_PREVIEW = "com.tencent.mm.plugin.gallery.ui.AlbumPreviewUI";
    public static final int GRID_VIEW = 2131627390;
    public static final String VIEW_CROP_IMAGE_NEW = "com.tencent.mm.ui.tools.CropImageNewUI";
    public static final int USE_NEW_ICON_BUTTON = 2131624139;
    public static final String VIEW_SETTING_SNS_BACKGROUND = "com.tencent.mm.plugin.sns.ui.SettingSnsBackgroundUI";//选择朋友圈封面来源
    public static final int SELECT_FROM_LOCAL_ID = 16908310;//从手机相册选择

    public static final String VIEW_TENCENT_NEWS = "com.tencent.mm.plugin.readerapp.ui.ReaderAppUI";//腾讯新闻
    public static final String VIEW_UPDATE_CONTACTS = "com.tencent.mm.ui.bindmobile.BindMContactIntroUI";//上传通讯录界面
    public static final int VIEW_REPORT_CONTACTS = 2131624441;
    public static final String VIEW_ADD_AREA = "com.tencent.mm.plugin.nearby.ui.NearbyPersonalInfoUI";//附近人选择地区
    public static final int VIEW_FEMALE = 1048577;
    public static final int VIEW_AREA = 16908310;
    public static final String VIEW_SELECT_CITY = "com.tencent.mm.ui.tools.MultiStageCitySelectUI";//选择地区界面

    public static final int VIEW_SELECT_CITY_LOCATION = 2131626724;
    public static final int VIEW_SHOW_CITY = 16908304;
    public static final int VIEW_SELECT_CITY_NEXT = 2131624138;
    public static final int VIEW_SELECT_SIGN = 16908304;

    public static final String VIEW_SELECT_PIC = "com.tencent.mm.plugin.gallery.ui.ImagePreviewUI";//选择图片界面
    public static final String VIEW_LUCKY_MONEY_RECEIVE = "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI";//红包
    public static final int RECEIVE_MONEY = 2131626389;
    public static final String VIEW_LUCKY_MONEY_DETAIL = "com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI";//红包详情
    public static final int RECEIVE_MONEY_NUM = 2131626294;
    public static final String VIEW_RECEIVE_MONEY = "com.tencent.mm.plugin.remittance.ui.RemittanceDetailUI";//接受转账
    public static final int SURE_RECEIVE_MONEY = 2131627298;
}
